import questiosAll from '../../utils/questions.js'
const app = getApp()
Page({
  data: {
    isIphoneX:false,
    autoplay: false,
    current: 0,
    interval: 1000,
    duration: 300,
    indicatorDots: false,
    rightAnasersNum: 0,
    allQuestion: questiosAll,
    num:8,
    questionsArr:[],
    currentActive:-1,
    helpNum:1,
    username:'',
    showMask: false,
    buttomBanner:null,
    buttomBannerImg:null,
    buttomBannerID:null,
    buttomBannerPath:null,
    buttomBannerUrl:null
  },
  onLoad: function (options) {
    var getAppInfo = app.globalData.userInfo;
    console.log(app.globalData.userInfo, getAppInfo)
    this.setData({
      username: getAppInfo.nickName
    })
    let questionsNums = this.data.allQuestion.length,
      randomArr = [],
      optionsRender=[],
      _this = this;
    do {
      let random_num = this.random(0, questionsNums - 1);
      if (randomArr.indexOf(random_num) == -1) {
        randomArr.push(random_num);
        _this.data.allQuestion[random_num].nowOptions = null;
        _this.data.allQuestion[random_num].clickDone = null;
        optionsRender.push(_this.data.allQuestion[random_num]);
      }
    } while (randomArr.length < this.data.num)
    this.setData({
       questionsArr:optionsRender
    })
  },
  random(lower, upper) {
    return Math.floor(Math.random() * (upper - lower + 1)) + lower;
  },
  onShow() {
    this.setData({
      isIphoneX: app.data.isIPhoneX
    })
    let _this=this;
    wx.request({
      url: app.data.commonUrl+'/Server/GameCenter/buttomBanner?appflag=xcx12',
      method: 'GET',
      success: function (res){    
        _this.setData({
          buttomBanner: res.data.result.type,
          buttomBannerID: res.data.result.appid
        })
        if (res.data.result.type=='image') {
          _this.setData({
            buttomBannerPath: res.data.result.path, 
            buttomBannerImg: res.data.result.url,
            buttomBannerUrl: res.data.result.case_url
          })
        }     
      }
    })
    
  },
  openUrl(){
    if (this.data.buttomBannerID != '' && this.data.buttomBanner=='image'){
      wx.navigateToMiniProgram({
        appId: this.data.buttomBannerID,
        path: this.data.buttomBannerPath,
        envVersion: 'release',
        fail(res) {
          wx.hideLoading();
        }
      })

    } else {
      wx.previewImage({
        current: this.data.buttomBannerUrl, 
        urls: [this.data.buttomBannerUrl] 
      })
     
    }
  },
  answerDone({ currentTarget }) {
    let { option, screenindex, optindex } = currentTarget.dataset,
        rightAnasersNum = this.data.rightAnasersNum,
        renderArr = this.data.questionsArr,
        current = this.data.current;
    renderArr[screenindex].choosen = option;
    this.setData({ 
      questionsArr: renderArr,
      currentActive: optindex
    });
    if (option === renderArr[screenindex].correct_option) {
      rightAnasersNum += 1;
      this.setData({ rightAnasersNum })
    }
    if (current <= this.data.questionsArr.length - 1) {
      current += 1;
      wx.showToast({
        title: option === renderArr[screenindex].correct_option ? `回答正确` : `回答错误`,
        icon: 'none',
        duration: 1000,
        mask: true,
        complete: res => {
          setTimeout(() => {
            if (current === this.data.questionsArr.length) {
              wx.reLaunch({
                url: `/pages/result/result?rightNum=${this.data.rightAnasersNum}`
              })
            } else {
              this.setData({ current })
            }
          }, 800)
        }
      })
    }
  },
  onShareAppMessage: function (res) {
    let help_num = this.data.helpNum,
        cur_num = this.data.current,
        right_num = this.data.rightAnasersNum,
        _this=this;
    if (res.target && res.target.dataset.flag == 'help'){
      let option = res.target.dataset.option,
          screenindex = res.target.dataset.screenindex,
          renderArr = this.data.questionsArr;
      renderArr[screenindex].choosen = option;
        return {
          title: '【' + this.data.username +'@你】求助大神，帮我看看这句东北话什么意思',
          path: `/pages/index/index`,
          success: function (res) {
            if (help_num >3 ) {
              wx.showToast({
                title: '只有三次求救机会喔',
                icon: 'none',
                duration: 2000,
                mask: true,
              })
              return false;
            }else{
              wx.showToast({
                title: '分享成功' + help_num +'/3',
                icon: 'success',
                duration: 1000,
                mask: true,
                success: function () {
                  help_num += 1;
                  _this.setData({
                    questionsArr: renderArr
                  });
                  setTimeout(() => {
                    cur_num += 1;
                    right_num += 1;
                    if (cur_num === _this.data.questionsArr.length) {
                      console.log(1111)
                      wx.reLaunch({
                        url: `/pages/result/result?rightNum=${right_num}`
                      })
                    } else {
                      _this.setData({ current: cur_num })
                    }
                    _this.setData({
                      helpNum: help_num,
                      rightAnasersNum: right_num
                    })
                  }, 1500)
                }
              })
            }           
          },
        }      
    }else{
      const index = ~~(Math.random() * app.data.shareTpls.length)
      return {
        title: app.data.shareTpls[index],
        path: `/pages/index/index`,
        imageUrl: app.data.img[index % app.data.img.length],
        success:function(){
        }
      }
    }  
  },
  toOtherMiniPro(){
    app.aldstat.sendEvent('更多测试', '点击')
    wx.navigateToMiniProgram({
      appId: 'wx011a82fedef36d59', // 要跳转的小程序的appid
      path: 'pages/index/index', // 跳转的目标页面
      extarData: {
        open: 'auth'
      },
      success(res) {
        // 打开成功  
      }
    })
  },
  showMaskFn() {
    this.setData({
      showMask: true
    })
  },
  maskGoOn() {
    this.setData({
      showMask: false
    })
  }

})
